<?php
/**
 * @author sanketsp
 * Course class: Holds data on all courses created by the instructor.
 */
class Course {
	function __construct($id) {
		$query = mysql_query("SELECT * FROM `course` WHERE `id` = '$id';") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			foreach($result as $key=>$value) {
				$this->$key = $value;
			}
		}
	}
	/**
	 * Update course details.
	 */
	private function updateCourse() {
		$query = mysql_query("UPDATE `course` SET `owner_id` = '$this->owner', `title` = '$this->title', `term` = '$this->term', `description` = '$this->description'") or die(mysql_error());
		return true;
	}
	/**
	 * Returns an array containing Instructor instances corresponding to each owner of the course.
	 */
	public function getAllOwners() {
		if(strlen($this->owner_id) == 1) {
			return new Instructor($this->owner_id);
		} else {
			$all = explode(',',$this->owner_id);
			foreach($all as $a) {
				$owners[] = new Instructor($a);
			}
			return $owners;
		}
	}
	/**
	 * Returns an array of Group instances present in the course.
	 */
	public function getAllGroups() {
		$query = mysql_query("SELECT * FROM `group` WHERE `course_id` = '$this->id'") or die(mysql_error());
		while($r = mysql_fetch_object($query)) {
			$groups[] = new Group($r->id);
		}
		return $groups;
	}
	/**
	 * Returns a single Group instance corresponding to the $id (Group ID) param.
	 */
	public function getGroup($id) {
		$query = mysql_query("SELECT * FROM `group` WHERE `id` = '$id'") or die(mysql_error());
		$r = mysql_fetch_object($query);
		if($r) {
			$group = new Group($r->id);
			return $group;
		} else {
			return false;
		}
	}
	/**
	 * Returns an array of Student instances present in the course.
	 */
	public function getAllStudents() {
		$groups = $this->getAllGroups();
		if($groups) {
			foreach($groups as $g) {
				$students = $g->getAllStudents();
				if($students) {
					foreach($students as $s) {
						$s->group_name = $g->name;
						$all[] = $s;
					}
				}
			}
		}
		if($all) {
			return $all;
		} else {
			return false;
		}
	}
	/**
	 * Adds another owner to the course.
	 * @param $owner_id requires the owner id
	 */
	public function addOwner($owner_id) {
		if(strlen($this->owner_id) == 1) {
			if($this->owner_id == $owner_id) {
				return false;
			} else {
				$this->owner_id .= ','.$owner_id;
			}
		} else {
			$all = explode(',',$this->owner_id);
			foreach($all as $key=>$value) {
				if($owner_id == $value)
				break;
			}
			$all[] = $owner_id;
			$this->owner_id = implode(',',$all);
		}
		$this->updateCourse();
	}
	/**
	 * Removes an owner from the course.
	 * @param $owner_id requires the owner id
	 */
	public function removeOwner($owner_id) {
		if(strlen($this->owner_id) == 1) {
			if($this->owner_id == $owner_id) {
				$this->owner_id = '';
			}
		} else {
			$all = explode(',',$this->owner_id);
			foreach($all as $key=>$value) {
				if($owner_id == $value) {
					unset($all[$key]);
					break;
				}
			}
			$this->owner_id = implode(',',$all);
		}
		$this->updateCourse();
	}
	/**
	 * Adds another Group to the course.
	 * @param $data OBJECT ARRAY
	 * @param $data->group_name Group Name,
	 * @param $data->group_description Group Description
	 * @param $data->google_code Google Code URL
	 * @param $data->google_groups Google Groups URL
	 */
	public function addGroup($data) {
		$query = mysql_query("SELECT MAX(id) FROM `group`") or die(mysql_error());
		$gid = mysql_fetch_row($query);
		$group_id = $gid[0] + 1;
		if($data->students != '') {
			for($i=0; $i<count($data->students); $i++) {
				$m = $data->students[$i];
				$full_name = $m->full_name? $m->full_name : "Invitation sent";
				$query = mysql_query("INSERT INTO `student` values
					(NULL,'$full_name','$m->gmail','$group_id');") or die(mysql_error());
			}
			$query = mysql_query("SELECT `id` FROM `student` WHERE `group_id` = '$group_id'") or die(mysql_error());
			while($r = mysql_fetch_row($query))
			$id[] = $r[0];
		}
		$ids = $id? trim(implode(',',$id)) : "NULL";
		$query = mysql_query("INSERT INTO `group` VALUES
			(NULL,'$data->group_name','$data->group_description','$ids',
			'$this->id','$data->google_code','$data->google_groups','0','0','0','0','0')") or die(mysql_error());
	}
	/**
	 * Remove a Group from the course.
	 * @param $id requires the group id
	 */
	public function removeGroup($id) {
		$group = $this->getGroup($id);
		if($group) {
			$group->remove();
			return true;
		} else {
			return false;
		}
	}
}